import {
	typeOf,
	getFormData,
	_getHttpCodeMsg,
	hasKey
} from '../shop/tools';

export {
	_getHttpCodeMsg,
	hasKey,
	typeOf,
	getFormData,
};

/*
httpConfig = {
	code: 'resultCode',
	msg: 'resultMsg',
	dataCode: 'data',
	timeout, retry, retryDelay
}
 */
let apiHost = "";
let errCodeFn,clearLogin,tipsMessage,errorResponse,getRequestUrl,getSign;
let httpConfig,errCode;

tipsMessage = getRequestUrl = getSign = ()=>{};
httpConfig = errCode = {};
/*
const errCodeFn = function(data,state){
	if(data.resultCode==5000) return data.resultMsg;
	else return data;
};
 */
export const installAPI = function(o = {}) {
    httpConfig = o.httpConfig || {}; 
    apiHost = o.apiHost || ""; //域名
    errCode = o.errCode || {	
    	sucess: 2000,
    	invalid: 100199,
    };

    if(typeOf(o.getRequestUrl)=='function') getRequestUrl = o.getRequestUrl; //获取http请求地址
    if(typeOf(o.getSign)=='function') getSign = o.getSign; //加签名
    // if(typeOf(o.getToken)=='function') getToken = o.getToken; //获取登录信息
    if(typeOf(o.clearLogin)=='function') clearLogin = o.clearLogin; // 清除登录信息
    if(typeOf(o.errCodeFn)=='function') errCodeFn = o.errCodeFn; //成功状态执行的方法（res）
    if(typeOf(o.errorResponse)=='function') errorResponse = o.errorResponse; //响应失败后执行的函数，没有则执行提示函数 (error)
    if(typeOf(o.tipsMessage)=='function') tipsMessage = o.tipsMessage; //提示函数
};

export const hasFileToFormData = (params = {}, fileData) => {
    return (fileData) ? getFormData(params,fileData) : params;
};

export {
	apiHost,
	httpConfig,
	errCode,

	getRequestUrl,
	getSign,
	// getToken,
	clearLogin,
	errCodeFn,
	errorResponse,
	tipsMessage
}